package com.glory.wxelectricitypda.adapter;

import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.glory.wxelectricitypda.R;
import com.glory.wxelectricitypda.bean.BACKShelfBean;

import java.util.ArrayList;
import java.util.List;


public class SameWorkorderlotsAdapter extends BaseQuickAdapter<BACKShelfBean.TESTLOTSDTO , BaseViewHolder> {
    private ArrayList<Integer> lotList = new ArrayList<>();
    private String lotid;
    public SameWorkorderlotsAdapter(@Nullable List<BACKShelfBean.TESTLOTSDTO> data) {
        super(R.layout.adapter_sameworkorderlots, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, BACKShelfBean.TESTLOTSDTO item) {
        helper.setText(R.id.tvLotId,item.getLOTID());
        helper.setText(R.id.tvMainQty,String.valueOf(item.getMAINQTY()));
        helper.setText(R.id.tvLocationID,item.getLOCATORID());
        helper.setText(R.id.stepName,item.getUDF().getRESERVED6());
        helper.setText(R.id.tvSubstrateId1,item.getSUBSTRATEID1());
        CheckBox checkbox = helper.getView(R.id.checkbox);
        if (item.getLOTID().equals(lotid)){
//            checkbox.setChecked(true);
            if (!lotList.contains(helper.getAdapterPosition())) {
                lotList.add(helper.getAdapterPosition());
            }
        }
        if (lotList.contains(helper.getAdapterPosition())){
            checkbox.setChecked(true);
        }else {
            checkbox.setChecked(false);
        }
        checkbox.setTag(helper.getAdapterPosition());
        checkbox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isChecked = ((CheckBox) v).isChecked();
                if (isChecked) {
                    lotList.add(helper.getAdapterPosition());
                } else {
                    int position = lotList.indexOf(helper.getAdapterPosition()); // 找到元素所在索引
                    if (position != -1) { // 确保元素存在
                        lotList.remove(position);
                        Log.d("aaa","aaa");
                    }
                }
            }
        });
    }

    public ArrayList<Integer> getLotList() {
        return this.lotList;
    }

    public void setLotid(String lotid) {
        this.lotid = lotid;
    }

}
